package com.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.reggie.common.BaseContext;
import com.reggie.common.R;
import com.reggie.dto.MyOrders;
import com.reggie.entity.OrderDetail;
import com.reggie.entity.Orders;
import com.reggie.entity.ShoppingCart;
import com.reggie.service.OrderDetailService;
import com.reggie.service.OrdersService;
import com.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/order")
@Slf4j
public class OrdersController {
    @Autowired
    private OrdersService ordersService;
    @Autowired
    private OrderDetailService orderDetailService;
    @Autowired
    private ShoppingCartService shoppingCartService;

    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders) {
        log.info("提价的下单数据：{}", orders.toString());
        ordersService.submit(orders);
        return R.success("支付");
    }

    /*
     * 查看订单
     * */
    @GetMapping("/userPage")
    public R<Page> userPage(int page, int pageSize) {
        log.info("查看订单前端提交的信息{}", page, pageSize);
        Page<MyOrders> myOrdersPage = ordersService.userPage(page, pageSize);
        return R.success(myOrdersPage);
    }

    @GetMapping("/list")
    public R<Page> historyList(int page, int pageSize) {
        Page<MyOrders> myOrdersPage = ordersService.historyList(page, pageSize);
        return R.success(myOrdersPage);
    }

    /*
     * 再来一单:应该是直接条到点餐页面，并且购物车里已经有了数据
     * 注意，传的参数是orders表中的主键id（和订单id会混淆）
     * */
    @PostMapping("/again")
    public R<String> again(@RequestBody Orders orders) {
        log.info("前端传过来的参数：{}", orders.toString());
        //获取orders中的主键
        Long id = orders.getId();
        //根据主键查询orders订单
        Orders order = ordersService.getById(id);
        //获取订单号，这样才能从order_detail中查询到改订单的详细
        String number = order.getNumber();
        LambdaQueryWrapper<OrderDetail> orderDetailLambdaQueryWrapper = new LambdaQueryWrapper<>();
        orderDetailLambdaQueryWrapper.eq(OrderDetail::getOrderId, number);
        List<OrderDetail> ordersList = orderDetailService.list(orderDetailLambdaQueryWrapper);
        //将订单明细表中的数据存入购物车
        for (OrderDetail orderDetail : ordersList) {
            ShoppingCart shoppingCart = new ShoppingCart();
            shoppingCart.setName(orderDetail.getName());
            shoppingCart.setImage(orderDetail.getImage());
            shoppingCart.setUserId(BaseContext.getCurrentId());
            shoppingCart.setDishId(orderDetail.getDishId());
            shoppingCart.setSetmealId(orderDetail.getSetmealId());
            shoppingCart.setDishFlavor(orderDetail.getDishFlavor());
            shoppingCart.setNumber(orderDetail.getNumber());
            shoppingCart.setAmount(orderDetail.getAmount());
            shoppingCart.setCreateTime(LocalDateTime.now());
            log.info("再来一单的菜品：{}", shoppingCart.toString());
            //保存到shopcart
            shoppingCartService.save(shoppingCart);

        }
        return R.success("再来一单成功");
    }

    /*
     * 后台的订单查询分页
     * */
    @GetMapping("/page")
    public R<Page> pageOrder(int page, int pageSize, Timestamp beginTime, Timestamp endTime,Long number) {
        log.info("页面信息：{}，{}，开始时间：{}，结束时间：{}",page,pageSize,beginTime,endTime);
        Page<Orders> pageInfo=new Page<>(page,pageSize);
        LambdaQueryWrapper<Orders> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.between(beginTime!=null&&endTime!=null,Orders::getOrderTime,beginTime,endTime)
                        .eq(number!=null,Orders::getNumber,number);
        ordersService.page(pageInfo,queryWrapper);
        return R.success(pageInfo);
    }


}
